package com.pro.db.service;

import com.pro.db.dao.BbsForumMapper;
import com.pro.db.domain.BbsForum;
import com.pro.db.domain.BbsTopic;
import com.pro.db.dao.BbsTopicMapper;
import com.pro.db.service.itf.IBbsTopicService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * <p>
 * 帖子表 服务实现类
 * </p>
 *
 * @author wanglei
 * @since 2020-01-07
 */
@Service
public class BbsTopicServiceImpl extends ServiceImpl<BbsTopicMapper, BbsTopic> implements IBbsTopicService {

    @Autowired
    private BbsTopicMapper topicMapper;

    @Autowired
    private BbsForumMapper forumMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveTopic(BbsTopic bbsTopic) {
        topicMapper.insert(bbsTopic);

        BbsForum forum = forumMapper.selectById(bbsTopic.getForumId());
        BbsForum update = new BbsForum();
        update.setId(forum.getId());
        update.setTopicNum(forum.getTopicNum()+1);
        update.setUpdateTime(LocalDateTime.now());
        forumMapper.updateById(update);
    }
}
